package com.zoro.barn.commons.webcommons.webconfig;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.ZoneId;
import java.util.List;

/**
 * @author zhaoxingwu
 */
public class JsonTimeZoneConfig implements WebMvcConfigurer {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        String FORMAT = "yyyy-MM-dd HH:mm:ss SSSS z Z";
        ObjectMapper mapper = Jackson2ObjectMapperBuilder.json()
                .timeZone(ZoneId.systemDefault().getId())
                .simpleDateFormat(FORMAT)
                .serializationInclusion(JsonInclude.Include.NON_NULL)
                .build();
        converters.add(0, new MappingJackson2HttpMessageConverter(mapper));
        logger.info("jackson message converter added , format as {}, no null field", FORMAT);
    }
}
